home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / perl / 5.10.0 / Text / Wrap.pm < prev   
Encoding:
Perl POD Document  |  2009-06-26  |  2.4 KB  |  117 lines

  1. package Text::Wrap;
  2.  
  3. use warnings::register;
  4. require Exporter;
  5.  
  6. @ISA = qw(Exporter);
  7. @EXPORT = qw(wrap fill);
  8. @EXPORT_OK = qw($columns $break $huge);
  9.  
  10. $VERSION = 2006.1117;
  11.  
  12. use vars qw($VERSION $columns $debug $break $huge $unexpand $tabstop
  13.     $separator $separator2);
  14. use strict;
  15.  
  16. BEGIN    {
  17.     $columns = 76;  # <= screen width
  18.     $debug = 0;
  19.     $break = '\s';
  20.     $huge = 'wrap'; # alternatively: 'die' or 'overflow'
  21.     $unexpand = 1;
  22.     $tabstop = 8;
  23.     $separator = "\n";
  24.     $separator2 = undef;
  25. }
  26.  
  27. use Text::Tabs qw(expand unexpand);
  28.  
  29. sub wrap
  30. {
  31.     my ($ip, $xp, @t) = @_;
  32.  
  33.     local($Text::Tabs::tabstop) = $tabstop;
  34.     my $r = "";
  35.     my $tail = pop(@t);
  36.     my $t = expand(join("", (map { /\s+\z/ ? ( $_ ) : ($_, ' ') } @t), $tail));
  37.     my $lead = $ip;
  38.     my $ll = $columns - length(expand($ip)) - 1;
  39.     $ll = 0 if $ll < 0;
  40.     my $nll = $columns - length(expand($xp)) - 1;
  41.     my $nl = "";
  42.     my $remainder = "";
  43.  
  44.     use re 'taint';
  45.  
  46.     pos($t) = 0;
  47.     while ($t !~ /\G(?:$break)*\Z/gc) {
  48.         if ($t =~ /\G([^\n]{0,$ll})($break|\n+|\z)/xmgc) {
  49.             $r .= $unexpand 
  50.                 ? unexpand($nl . $lead . $1)
  51.                 : $nl . $lead . $1;
  52.             $remainder = $2;
  53.         } elsif ($huge eq 'wrap' && $t =~ /\G([^\n]{$ll})/gc) {
  54.             $r .= $unexpand 
  55.                 ? unexpand($nl . $lead . $1)
  56.                 : $nl . $lead . $1;
  57.             $remainder = defined($separator2) ? $separator2 : $separator;
  58.         } elsif ($huge eq 'overflow' && $t =~ /\G([^\n]*?)($break|\n+|\z)/xmgc) {
  59.             $r .= $unexpand 
  60.                 ? unexpand($nl . $lead . $1)
  61.                 : $nl . $lead . $1;
  62.             $remainder = $2;
  63.         } elsif ($huge eq 'die') {
  64.             die "couldn't wrap '$t'";
  65.         } elsif ($columns < 2) {
  66.             warnings::warnif "Increasing \$Text::Wrap::columns from $columns to 2";
  67.             $columns = 2;
  68.             return ($ip, $xp, @t);
  69.         } else {
  70.             die "This shouldn't happen";
  71.         }
  72.             
  73.         $lead = $xp;
  74.         $ll = $nll;
  75.         $nl = defined($separator2)
  76.             ? ($remainder eq "\n"
  77.                 ? "\n"
  78.                 : $separator2)
  79.             : $separator;
  80.     }
  81.     $r .= $remainder;
  82.  
  83.     print "-----------$r---------\n" if $debug;
  84.  
  85.     print "Finish up with '$lead'\n" if $debug;
  86.  
  87.     $r .= $lead . substr($t, pos($t), length($t)-pos($t))
  88.         if pos($t) ne length($t);
  89.  
  90.     print "-----------$r---------\n" if $debug;;
  91.  
  92.     return $r;
  93. }
  94.  
  95. sub fill 
  96. {
  97.     my ($ip, $xp, @raw) = @_;
  98.     my @para;
  99.     my $pp;
  100.  
  101.     for $pp (split(/\n\s+/, join("\n",@raw))) {
  102.         $pp =~ s/\s+/ /g;
  103.         my $x = wrap($ip, $xp, $pp);
  104.         push(@para, $x);
  105.     }
  106.  
  107.     # if paragraph_indent is the same as line_indent, 
  108.     # separate paragraphs with blank lines
  109.  
  110.     my $ps = ($ip eq $xp) ? "\n\n" : "\n";
  111.     return join ($ps, @para);
  112. }
  113.  
  114. 1;
  115. __END__
  116.  
  117.